#
# script.wold - XBMC Wake on LAN
#
# Copyright (C) 2012 Adam Sutton <dev@adamsutton.me.uk>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

# ############################################################################
# Module Setup/Info
# #############################################################################

# Global imports
import os, sys, time
import xbmcaddon

# Addon info
__addon__     = xbmcaddon.Addon()                 
__addonname__ = __addon__.getAddonInfo('name')
__cwd__       = __addon__.getAddonInfo('path')
__author__    = __addon__.getAddonInfo('author')
__version__   = __addon__.getAddonInfo('version')
__language__  = __addon__.getLocalizedString
sys.path.append(xbmc.translatePath(os.path.join(__cwd__, 'resources', 'lib')))

# Local imports
import wol

# ############################################################################
# Functions
# #############################################################################

#
# Logging (TODO: move)
#
def log ( msg ):
  xbmc.log('%s - %s' % (__addonname__, msg))

# ############################################################################
# Main
# #############################################################################
  
# Addon started
log('run')
log('author:  %s' % __author__)
log('version: %s' % __version__)

# Get MACs
macs   = []
for i in range(3):
  m = __addon__.getSetting('mac%d' % i).upper()
  if wol.is_mac(m): macs.append(m)

# Send
port   = int(__addon__.getSetting('port'))
repeat = int(__addon__.getSetting('repeat'))
if repeat < 1: repeat = 1
for m in macs:
  log('send to %s' % m)
  wol.send(m, port, repeat)

# Terminated
log('done')
